package com.example.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

public class TestGet {
        public static void main(String[] args) throws Exception{
            // TODO Auto-generated method stub
            //1,创建一个httpClient对象
            CloseableHttpClient client= HttpClients.createDefault();
            //2,创建uriBuilder 对于httpClient4.3访问指定页面url必须要使用http://开始
            URIBuilder uriBuilder=new URIBuilder("http://www.baidu.com");
            //4,创建httpget对象
            HttpGet httpGet=new HttpGet(uriBuilder.build());
            //5,设置请求报文头部的编码
            httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"));
            //6,设置期望服务返回的编码
            httpGet.setHeader(new BasicHeader("Accept","text/plain;charset=utf-8"));
            //7，请求服务
            CloseableHttpResponse response=client.execute(httpGet);
            //8，获取请求返回码
            int statusCode=response.getStatusLine().getStatusCode();
            //9如果请求返回码是200，则说明请求成功
            if(statusCode==200){
                //10,获取返回实体
                HttpEntity entity=response.getEntity();
                //11,通过EntityUtils的一个工具类获取返回的内容
                String str= EntityUtils.toString(entity,"utf-8");
                System.out.println("请求成功的返回内容："+str);

            }else{
                System.out.println("请求失败！");
            }
            response.close();
            client.close();
        }
}
